//
//  NewBuyVC.swift
//  LeGou
//
//  Created by 刘亿前 on 2022/9/16.
//

import UIKit
import LTScrollView

class NewBuyVC: ZMViewController {
    
    @IBOutlet weak var navHeight: NSLayoutConstraint!
    
    @IBOutlet weak var ruleBtn: UIButton!
    private lazy var headView: UIView = {
        let view = UIView(frame: CGRect(x: -12, y: 0, width: SCREEN_W, height: 270))
        view.backgroundColor = .clear
        let img = UIImageView(frame: CGRect(x: 0, y: 0, width: SCREEN_W, height: 260))
        img.image = UIImage(named: "new_buy_bg")
        view.addSubview(img)
        return view
    }()
    
    private lazy var layout: LTLayout = {
        let layout = LTLayout()
        layout.titleViewBgColor = UIColor(r: 187, g: 34, b: 32)
        layout.titleSelectColor = UIColor(r: 255, g: 255, b: 255)
        layout.bottomLineColor = UIColor(r: 255, g: 255, b: 255)
        layout.pageBottomLineColor = .clear
        layout.titleColor = UIColor.zm.color(hexString: "#FFFFFF", 0.75)
        layout.lrMargin = 12
        layout.sliderWidth = 16
        layout.titleMargin = 36
        layout.isNeedScale = false
        layout.titleFont = UIFont.zm.fontMedium(size: 16)
        layout.showsHorizontalScrollIndicator = false
        return layout
    }()
        
    private lazy var controllers: [NewBuyListVC] = {
        var arr = [NewBuyListVC]()
        cateArr.forEach { val in
            let vc = NewBuyListVC()
            vc.cid = val.id
            arr.append(vc)
        }
        return arr
    }()
    
    private lazy var advancedManager: LTAdvancedManager = {
        let manager = LTAdvancedManager(frame: CGRect(x: 12, y: StaNavBarH, width: SCREEN_W - 24, height: SCREEN_H-StaNavBarH), viewControllers: controllers, titles: cateArr.map{return $0.name}, currentViewController: self, layout: layout, headerViewHandle: {
            let img = UIImageView(frame: CGRect(x: -12, y: 0, width: SCREEN_W, height: 270))
            img.image = UIImage(named: "new_buy_bg")
            return headView
        })
        manager.backgroundColor = .clear
        return manager
    }()
    
    var cateArr = [GoodsCategory]()

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "新人免单"
        requestCategory()
        hbd_barAlpha = 0
        navHeight.constant = StaNavBarH
        ruleBtn.zm.filletedCorner(14, [.topLeft, .bottomLeft])
        view.backgroundColor = "#A90403".zm.hex()
        bindAction()
    }
    
    // MARK: - bindAction
    func bindAction() {
        ruleBtn.rx.tap.subscribe(onNext:{ resp in
            CommonService.shared.goWeb(type: .newBuy)
        }).disposed(by: disposeBag)
    }
}

// MARK: - Request Action
extension NewBuyVC {
    func requestCategory() {
        ZMToast.showActivity()
        HomeAPI.tb_goods_cate.rxRequestNullAble(type: [GoodsCategory].self).subscribe(onNext:{ [unowned self] resp in
            ZMToast.hiddenActivity()
            if let data = resp {
                self.cateArr = data
                let all = GoodsCategory(name: "全部", id: 0)
                self.cateArr.insert(all, at: 0)
                self.view.insertSubview(advancedManager, at: 0)
            } else {
                ZMToast.showToast(title: "暂无分类")
                self.navigationController?.popViewController(animated: true)
            }
        }).disposed(by: disposeBag)
    }
}
